package com.castle.fortress.admin.pay.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.pay.dto.PaySceneDto;
import com.castle.fortress.admin.pay.entity.PayAliConfigEntity;
import com.castle.fortress.admin.pay.entity.PaySceneEntity;
import com.castle.fortress.admin.pay.entity.PayWxConfigEntity;
import com.castle.fortress.admin.pay.enums.PayPlatformEnum;
import com.castle.fortress.admin.pay.enums.PayTypeEnum;
import com.castle.fortress.admin.pay.mapper.PaySceneMapper;
import com.castle.fortress.admin.pay.service.PayAliConfigService;
import com.castle.fortress.admin.pay.service.PaySceneService;
import com.castle.fortress.admin.pay.service.PayWxConfigService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 支付场景表 服务实现类
 *
 * @author castle
 * @since 2022-07-07
 */
@Service
public class PaySceneServiceImpl extends ServiceImpl<PaySceneMapper, PaySceneEntity> implements PaySceneService {
    @Autowired
    private PayWxConfigService payWxConfigService;
    @Autowired
    private PayAliConfigService payAliConfigService;
    /**
    * 获取查询条件
    * @param paySceneDto
    * @return
    */
    public QueryWrapper<PaySceneEntity> getWrapper(PaySceneDto paySceneDto){
        QueryWrapper<PaySceneEntity> wrapper= new QueryWrapper();
        if(paySceneDto != null){
            PaySceneEntity paySceneEntity = ConvertUtil.transformObj(paySceneDto,PaySceneEntity.class);
            wrapper.eq(StrUtil.isNotEmpty(paySceneEntity.getCode()),"code",paySceneEntity.getCode());
            wrapper.like(StrUtil.isNotEmpty(paySceneEntity.getName()),"name",paySceneEntity.getName());
        }
        return wrapper;
    }


    @Override
    public IPage<PaySceneDto> pagePayScene(Page<PaySceneDto> page, PaySceneDto paySceneDto) {
        QueryWrapper<PaySceneEntity> wrapper = getWrapper(paySceneDto);
        Page<PaySceneEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<PaySceneEntity> payScenePage=baseMapper.selectPage(pageEntity,wrapper);
        Page<PaySceneDto> pageDto = new Page(payScenePage.getCurrent(), payScenePage.getSize(),payScenePage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(payScenePage.getRecords(),PaySceneDto.class));
        return pageDto;
    }


    @Override
    public List<PaySceneDto> listPayScene(PaySceneDto paySceneDto){
        QueryWrapper<PaySceneEntity> wrapper = getWrapper(paySceneDto);
        List<PaySceneEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,PaySceneDto.class);
    }

    @Override
    public RespBody checkColumnRepeat(PaySceneDto dto) {
        //校验code
        QueryWrapper<PaySceneEntity> queryWrapper=new QueryWrapper();
        queryWrapper.eq("code",dto.getCode());
        List<PaySceneEntity> list = baseMapper.selectList(queryWrapper);
        if(list != null && !list.isEmpty()){
            //新增
            if(dto.getId() == null){
                return RespBody.fail(BizErrorCode.PAY_SCENE_CODE_EXIST_ERROR);
                //修改
            }else{
                for(PaySceneEntity entity:list){
                    if(!entity.getId().equals(dto.getId())){
                        return RespBody.fail(BizErrorCode.PAY_SCENE_CODE_EXIST_ERROR);
                    }
                }
            }
        }
        return RespBody.data("校验通过");
    }
    @Async
    @Override
    public void asynSaveConfig(PaySceneEntity paySceneEntity) {
        List<PayWxConfigEntity> wxList = new ArrayList<>();
        List<PayAliConfigEntity> aliList = new ArrayList<>();
        for(PayTypeEnum typeEnum:PayTypeEnum.values()){
            if(PayPlatformEnum.ALIPAY==typeEnum.getPlatform()){
                PayAliConfigEntity entity= new PayAliConfigEntity();
                entity.setPayTypeCode(typeEnum.getCode());
                entity.setStatus(3);//不可用
                entity.setSceneCode(paySceneEntity.getCode());
                aliList.add(entity);
            }else if(PayPlatformEnum.WX==typeEnum.getPlatform()){
                PayWxConfigEntity entity= new PayWxConfigEntity();
                entity.setPayTypeCode(typeEnum.getCode());
                entity.setStatus(3);//不可用
                entity.setSceneCode(paySceneEntity.getCode());
                wxList.add(entity);
            }
        }
        payWxConfigService.saveBatch(wxList);
        payAliConfigService.saveBatch(aliList);
    }
    @Async
    @Override
    public void asynDelConfig(PaySceneEntity paySceneEntity) {
        QueryWrapper<PayWxConfigEntity> wx = new QueryWrapper<>();
        wx.eq("scene_code",paySceneEntity.getCode());
        payWxConfigService.remove(wx);
        QueryWrapper<PayAliConfigEntity> ali = new QueryWrapper<>();
        ali.eq("scene_code",paySceneEntity.getCode());
        payAliConfigService.remove(ali);
    }
}

